<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Site extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->library('Template');
		require_once 'Mobile_Detect.php';
		$detect = new Mobile_Detect;
		$mobile = array('isMobile' => $detect->isMobile() == 1 ? true : false,
						'isTablet' => $detect->isTablet() == 1 ? true : false,
						'isPhone' => ($detect->isMobile() && !$detect->isTablet()) == 1 ? true : false,
						'isIE7' => ($detect->version('MSIE')==7.0) == 1 ? true : false,
						'isIE8' => ($detect->version('MSIE')==8.0) == 1 ? true : false,
						'isIE9' => ($detect->version('MSIE')==9.0) == 1 ? true : false
				);
		$this->load->vars($mobile);
		
	}
	
	function example()
	{
		 
		$data = array();
		 
		$this->template->set_template('layout');
		$this->template->write_view('contentRegion', 'pages/example', $data, TRUE);
	
		$layout_message = $this->template->render('', TRUE);
		 
		$this->template->set_template('base');
		$this->addImports();
		$this->template->write('layoutRegion', $layout_message);
		$this->template->render();
		 
	}
	
	
   	function index()
   	{
   		
   		$data = array('name' => 'User', 'birthdate' => '01/01/1970');
   		
   		$this->template->set_template('layout');
   		$this->addNavbar($data);
   		$this->template->write_view('contentRegion', 'pages/home', $data, TRUE);
   		$this->addFooter($data);

   		$layout_message = $this->template->render('', TRUE);
   		
   		$this->template->set_template('base');
   		$this->addImports();
   		$this->template->write('layoutRegion', $layout_message);
      	$this->template->render();
      	
   	}
   	
   	function gallery()
   	{
   		$this->load->helper('directory');
   		$map = directory_map('./img/gallery/full/');
   		 
   		$data =  array();
   		 
   		$imgSet =  array('map'=> $map);
   		$this->load->vars($imgSet);
   		 
   		$this->template->set_template('layout');
   		$this->addNavbar($data);
   		$this->template->write_view('contentRegion', 'pages/gallery', $data, TRUE);
   		$this->addFooter($data);
   		$layout_message = $this->template->render('', TRUE);
   		 
   		$this->template->set_template('base');
   		$this->addImports();
   		$this->template->write('titleRegion', $this->site['avTitle']);
   		$this->template->write('layoutRegion', $layout_message);
   		$this->template->render();
   	}
   	
   	function noload() {
   		function endsWith($haystack, $needle)
   		{
   			$length = strlen($needle);
   			if ($length == 0) {
   				return true;
   			}
   		  
   			return (substr($haystack, -$length) === $needle);
   		}
   		 
   		$name = 'img/loader.gif';
   		$fp = fopen($name, 'rb');
   	
   		header("Content-Type: image/gif");
   		header("Content-Length: " . filesize($name));
   	
   		fpassthru($fp);
   	}
   	
   	function addNavbar($data) {
   		$this->template->write_view('navbarPhoneRegion', 'components-phone/navbar', $data, TRUE);
   		$this->template->write_view('navbarTabletRegion', 'components-tablet/navbar', $data, TRUE);
   		$this->template->write_view('navbarDesktopRegion', 'components-desktop/navbar', $data, TRUE);
   	}
   	
   	function addFooter($data) {
   		$this->template->write_view('footerPhoneRegion', 'components-phone/footer', $data, TRUE);
   		$this->template->write_view('footerTabletRegion', 'components-tablet/footer', $data, TRUE);
   		$this->template->write_view('footerDesktopRegion', 'components-desktop/footer', $data, TRUE);
   	}
   	
   	function addImports() {
   		$detect = new Mobile_Detect;
   		$this->template->set_template('base');
   		//including general CSS libraries
   		$this->template->add_css('css/bootstrap.min.css');
   		$this->template->add_css('css/normalize.css');
   		$this->template->add_css('css/averonesi.css');
   		
   		if ($detect->isMobile()) {
   			//mobile
   			$this->template->add_css('css/averonesi-mobile.css');
   		}
   		if($detect->isTablet()) {
   			//tablet
   			$this->template->add_css('css/averonesi-phone.css');
   			$this->template->add_css('css/averonesi-tablet.css');
   			$this->template->add_css('css/averonesi-desktop.css');
   		} else if ($detect->isMobile() && !$detect->isTablet()) {
   			//phone
   			$this->template->add_css('css/averonesi-phone.css');
   		} else {
   			//computer
   			if($detect->version('MSIE')==7.0) {
   				$this->template->add_css('css/averonesi-ie7.css');
   			} else if($detect->version('MSIE')==8.0) {
   				$this->template->add_css('css/averonesi-ie8.css');
   			} else if($detect->version('MSIE')==9.0) {
   				$this->template->add_css('css/averonesi-ie9.css');
   			} else {
   				$this->template->add_css('css/averonesi-desktop.css');
   			}
   		}
   		
   		
   		
   		//including general JS libraries
   		$this->template->add_js('js/jquery-1.9.1.min.js');
   		$this->template->add_js('js/modernizr-2.6.2.min.js');
   		$this->template->add_js('js/yepnope.1.5.4-min.js');
   		$this->template->add_js('js/respond.min.js');
   		$this->template->add_js('js/plugins.js');
   		$this->template->add_js('js/main.js');
   		
   		if ($detect->isMobile()) {
   			//mobile
   			$this->template->add_js('js/averonesi-mobile.js');
   		}
   		if($detect->isTablet()) {
   			//tablet
   			$this->template->add_js('js/averonesi-phone.js');
   			$this->template->add_js('js/averonesi-tablet.js');
   			$this->template->add_js('js/averonesi-desktop.js');
   		} else if ($detect->isMobile() && !$detect->isTablet()) {
   			//phone
   			$this->template->add_js('js/averonesi-phone.js');
   		} else {
   			//computer
   			if($detect->version('MSIE')==7.0) {
   				$this->template->add_js('js/averonesi-ie7.js');
   			} else if($detect->version('MSIE')==8.0) {
   				$this->template->add_js('js/averonesi-ie8.js');
   			} else if($detect->version('MSIE')==9.0) {
   				$this->template->add_js('js/averonesi-ie9.js');
   			} else {
   				$this->template->add_js('js/averonesi-desktop.js');
   			}
   		}
   	}

}